package com.example.sudokugame

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.sudokugame.adapters.LeaderboardAdapter
import com.example.sudokugame.databinding.ActivityLeaderboardBinding
import com.example.sudokugame.models.Difficulty
import com.example.sudokugame.models.LeaderboardEntry
import com.example.sudokugame.network.NetworkManager
import com.google.android.material.tabs.TabLayout
import kotlinx.coroutines.launch

class LeaderboardActivity : AppCompatActivity() {
    
    private lateinit var binding: ActivityLeaderboardBinding
    private lateinit var adapter: LeaderboardAdapter
    private var currentDifficulty = Difficulty.EASY
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLeaderboardBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
        setupToolbar()
        setupRecyclerView()
        setupTabs()
        loadLeaderboard()
    }
    
    private fun setupToolbar() {
        binding.toolbar.setNavigationOnClickListener {
            finish()
        }
    }
    
    private fun setupRecyclerView() {
        adapter = LeaderboardAdapter()
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter
    }
    
    private fun setupTabs() {
        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                when (tab?.position) {
                    0 -> currentDifficulty = Difficulty.EASY
                    1 -> currentDifficulty = Difficulty.MEDIUM
                    2 -> currentDifficulty = Difficulty.HARD
                    3 -> currentDifficulty = Difficulty.EXPERT
                }
                loadLeaderboard()
            }
            
            override fun onTabUnselected(tab: TabLayout.Tab?) {}
            override fun onTabReselected(tab: TabLayout.Tab?) {}
        })
        
        binding.btnRefresh.setOnClickListener {
            loadLeaderboard()
        }
    }
    
    private fun loadLeaderboard() {
        showLoading()
        
        lifecycleScope.launch {
            try {
                // 使用正确的API调用，包含type参数
                val response = NetworkManager.apiService.getLeaderboard(
                    type = "score", // 按分数排行
                    difficulty = currentDifficulty.value,
                    limit = 50
                )
                
                if (response.isSuccessful && response.body() != null) {
                    val leaderboardData = response.body()!!.data.leaderboard
                    
                    if (leaderboardData.isNotEmpty()) {
                        adapter.updateData(leaderboardData)
                        showContent()
                    } else {
                        showEmptyState()
                    }
                } else {
                    showEmptyState()
                    Toast.makeText(this@LeaderboardActivity, "加载排行榜失败", Toast.LENGTH_SHORT).show()
                }
            } catch (e: Exception) {
                showEmptyState()
                Toast.makeText(this@LeaderboardActivity, "网络错误：${e.message}", Toast.LENGTH_SHORT).show()
            }
        }
    }
    
    private fun showLoading() {
        binding.progressBar.visibility = View.VISIBLE
        binding.recyclerView.visibility = View.GONE
        binding.emptyState.visibility = View.GONE
    }
    
    private fun showContent() {
        binding.progressBar.visibility = View.GONE
        binding.recyclerView.visibility = View.VISIBLE
        binding.emptyState.visibility = View.GONE
    }
    
    private fun showEmptyState() {
        binding.progressBar.visibility = View.GONE
        binding.recyclerView.visibility = View.GONE
        binding.emptyState.visibility = View.VISIBLE
    }
} 